//
//  PPAlbumPictureInsertionPictureStatusView.m
//  pickpic
//
//  Created by Jinxiao on 10/15/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAssetsPictureViewStatusBar.h"

@interface PPAssetsPictureViewStatusBar ()
@property (readwrite, nonatomic, strong) UIButton *selectButton;
@property (readwrite, nonatomic, strong) UIButton *doneButton;
@property (readwrite, nonatomic, strong) UILabel *countLabel;
@end

@implementation PPAssetsPictureViewStatusBar

- (instancetype)init {
    self = [super init];

    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectButton.adjustsImageWhenHighlighted = NO;
    _selectButton.exclusiveTouch = YES;
    _selectButton.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    [_selectButton setImage:[UIImage imageNamed:@"radio-select-36-white"] forState:UIControlStateNormal];
    [_selectButton setImage:[UIImage imageNamed:@"radio-select-36"] forState:UIControlStateSelected|UIControlStateHighlighted];
    [_selectButton setImage:[UIImage imageNamed:@"radio-select-36"] forState:UIControlStateSelected];
    [_selectButton sizeToFit];
    [_selectButton addTarget:self action:@selector(selectButtonDidPressed) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_selectButton];

    _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _doneButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _doneButton.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [_doneButton setTitleColor:[UIColor colorWithRGBHex:0xffdd17] forState:UIControlStateNormal];
    [_doneButton setTitleColor:[UIColor colorWithRGBHex:0xb2b2b2] forState:UIControlStateDisabled];
    [_doneButton sizeToFit];
    [_doneButton addTarget:self action:@selector(doneButtonDidPressed) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_doneButton];

    _countLabel = [[UILabel alloc] init];
    _countLabel.backgroundColor = [UIColor colorWithRGBHex:0xffdd17];
    _countLabel.textColor = [UIColor blackColor];
    _countLabel.font = [UIFont systemFontOfSize:9];
    _countLabel.textAlignment = NSTextAlignmentCenter;
    _countLabel.layer.cornerRadius = 7.5;
    _countLabel.layer.masksToBounds = YES;
    [self addSubview:_countLabel];

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _selectButton.left = 0;
    _selectButton.centerY = self.height/2;

    _selectButton.selected = _selected;

    _doneButton.right = self.width;
    _doneButton.centerY = self.height/2;

    [_countLabel sizeToFit];
    _countLabel.width += 6;
    _countLabel.height = 15;
    _countLabel.width = MAX(_countLabel.width, 15);
    _countLabel.centerY = self.height/2;
    _countLabel.right = _doneButton.left - 1;
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;

    [self setNeedsLayout];
}

- (void)setCount:(NSInteger)count {
    _count = count;

    _countLabel.text = [NSString stringWithFormat:@"%li", (long)count];

    _countLabel.hidden = count == 0;

    _doneButton.enabled = count > 0;

    [self setNeedsLayout];
}

- (void)selectButtonDidPressed {
    [_delegate pictureStatusBarDidPressSelectButton:self];
}

- (void)doneButtonDidPressed {
    [_delegate pictureStatusBarDidPressDoneButton:self];
}

@end
